import { BasicDrawer, useDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicForm, FormSchema, useForm } from "@/components/Form";
import { PurPlanApi } from "@/api/erp/purchase/purPlan";
import { message } from "antd";
import { useTranslation } from "react-i18next";
import { useState, useMemo } from "react";
import { productApi } from "@/api/erp/drug/drug";
import { SupplierApi } from "@/api/erp/purchase/supplier";
import { BidDrugTypeApi } from "@/api/erp/bid/bidDrugType";
import StockInFormDetail from "./StockInFormDetail";
let reload: Function;
let id: any;
const index = (props: { register: Fn }) => {
	const [isUpdated, setIsUpdated] = useState(false);
	const { t } = useTranslation();
	const en_vi = "drugStorage.purchasingManagement.other";
	const formSchema: FormSchema[] = [
		{
			label: t("drugStorage.productName"),
			component: "ApiSelect",
			field: "drugId",
			componentProps: {
				api: productApi.getproductSimpleList,
				labelField: "productName",
				valueField: "productId",
				immediate: false,
				params: {
					bizType: "1"
				}
			}
		},
		{
			label: t(`${en_vi}.contractCat`),
			component: "ApiSelect",
			field: "contractCat",
			componentProps: {
				api: BidDrugTypeApi.getBidDrugTypePage,
				labelField: "name",
				valueField: "id",
				immediate: false
			}
		},
		{
			label: t(`${en_vi}.manufacturerId`),
			component: "ApiSelect",
			field: "manufacturerId",
			componentProps: {
				api: SupplierApi.getSupplierSimpleList,
				labelField: "name",
				valueField: "suplerId",
				immediate: false
			}
		},
		{
			label: t(`${en_vi}.contractNo`),
			field: "contractNo",
			component: "InputSearch",
			componentProps: {
				onSearch: () => openDrawer(true)
			}
		},
		{
			label: t(`${en_vi}.totalStock`),
			field: "totalStock",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.stockQty`),
			field: "stockQty",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.requiredQty`),
			field: "requiredQty",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.specifica`),
			field: "specifica",
			component: "Input"
		},
		{
			label: t(`${en_vi}.execMeth`),
			field: "execMeth",
			component: "Input"
		},
		{
			label: t(`${en_vi}.purPrice`),
			field: "purPrice",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.companyId`),
			field: "companyId",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.date`),
			field: "date",
			component: "DatePicker",
			componentProps: {
				picker: "year"
			}
		},
		{
			label: t(`${en_vi}.taxRate`),
			field: "taxRate",
			component: "InputNumber"
		}
	];
	const getTitle = useMemo(() => {
		return isUpdated ? t("felix_ui.edit") : t("felix_ui.add");
	}, [isUpdated]);
	const [register, { validateFields, setFieldsValue, resetFields, setFieldValue }] = useForm({
		schemas: formSchema,
		labelWidth: 150,
		baseColProps: {
			span: 24
		}
	});
	const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(props.register, data => {
		reload = data.reload;
		id = data.record?.id;
		resetFields();
		setDrawerProps({ confirmLoading: true });
		setIsUpdated(!!data?.isUpdated);
		setFieldsValue({
			...data.record
		});
		setDrawerProps({ confirmLoading: false });
	});
	const handleSubmit = async () => {
		const formValue = await validateFields();
		formValue.date = formValue.date?.substring(0, 4);
		let res;
		if (isUpdated) {
			formValue.id = id;
			res = await PurPlanApi.updatePurPlan(formValue);
		} else {
			res = await PurPlanApi.createPurPlan(formValue);
		}
		if (!res) {
			message.error(t("viewComponents.patientInformation.additionFailed"));
			return;
		}
		message.success(t("viewComponents.patientInformation.successfullyAdded"));
		closeDrawer();
		reload();
	};
	const [drawerDetail, { openDrawer }] = useDrawer();
	const setOrderItem = (contractNo: number) => {
		setFieldValue("contractNo", contractNo);
	};
	return (
		<>
			<BasicDrawer title={getTitle} showFooter={true} {...props} register={registerDrawer} ok={handleSubmit}>
				<BasicForm register={register} labelWrap={true}></BasicForm>
			</BasicDrawer>
			<StockInFormDetail register={drawerDetail} setOrderItem={setOrderItem}></StockInFormDetail>
		</>
	);
};
export default index;
